package com.fwixvye;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fwixvye.mapper.MultiplierMapper;
import com.fwixvye.pojo.Multiplier;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Wrapper;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SpringBootTest
public class DatabaseInsertionDataTest2 {

    @Autowired
    MultiplierMapper multiplierMapper;


    /**
     * 将每个角色的网址页面放入到
     * D:\JavaWebProjects\AWoodenStakeHd\src\main\resources\txt\charactersWebsitePage.txt
     */
    @Test
    void webPageToTxt() throws IOException {
        /*
            角色筛选的网址，用来获取每个角色的地址
            https://wiki.biligame.com/ys/角色筛选
        */
        URL url = new URL("https://wiki.biligame.com/ys/%E8%A7%92%E8%89%B2%E7%AD%9B%E9%80%89");
        URLConnection connection = url.openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
//        System.out.println(sb);


        /*
            通过正则表达式拿到每个角色的网页地址，存放在 list2 中
        */
        Pattern compile1 = Pattern.compile("<td class=\"hidden-xs\"><a href=\"([^\"]+)\"");
        Matcher matcher1 = compile1.matcher(sb);
        ArrayList<String> list1 = new ArrayList<>();
        while (matcher1.find()) {
            list1.add(matcher1.group());
        }

        ArrayList<String> list2 = new ArrayList<>();
        for (String s : list1) {
            Pattern compile2 = Pattern.compile("\"/([^\"]+)\"");
            Matcher matcher2 = compile2.matcher(s);
            while (matcher2.find()) {
                list2.add(("https://wiki.biligame.com" + matcher2.group()).replace("\"", ""));
            }
        }
//        System.out.println(list2);


        /*
            list2：存放每个角色网址的集合
            遍历集合，获取里面的表格
            list3：存放每个角色的网页数据
        */
        //ArrayList<String> list3 = new ArrayList<>();
        File file = new File("D:\\JavaWebProjects\\AWoodenStakeHd\\src\\main\\resources\\txt\\charactersWebsitePage.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (String s : list2) {
            URL urlRole = new URL(s);
            URLConnection connectionRole = urlRole.openConnection();
            BufferedReader brRole = new BufferedReader(new InputStreamReader(connectionRole.getInputStream()));
            String lineRole;
            while ((lineRole = brRole.readLine()) != null) {
                bw.write(lineRole);

            }
            brRole.close();
            bw.write("\n");
            bw.write("!@#!@#!@#!@#!@#");

        }
        bw.close();
    }

    /**
     * 处理TXT文件
     *
     * @throws IOException 0
     */
    @Test
    void txtToList() throws IOException {
        /*
            获取txt文件放到list集合中
        */
        File file = new File("D:\\JavaWebProjects\\AWoodenStakeHd\\src\\main\\resources\\txt\\charactersWebsitePage.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        String string = sb.toString();
        String[] strings = string.split("!@#!@#!@#!@#!@#");
        ArrayList<String> list = new ArrayList<>(Arrays.asList(strings));
//        System.out.println(list.size());






        /*
            正则表达式为：
            <div style="position: relative; overflow-x: scroll; border: 1px solid #bdbdbd;"><table class="wikitable" style="width:100%;max-width: 1300px; margin:0px;word-break: keep-all; border-collapse: separate;border-spacing:0; border:0;position:relative;">
            [\w\W]*?LV13[\w\W]*?
            </div><table class="wikitable mw-collapsible mw-collapsed" style="width:100%;display:none">
        */
//        System.out.println(list.get(0));
        Pattern compile3 = Pattern.compile("<meta itemprop=\"name\" content=\"[\\u4e00-\\u9fa5]+ - 原神WIKI_BWIKI_哔哩哔哩\"/>");
        Matcher matcher3 = compile3.matcher(list.get(0));
        String roleName = null;
        while (matcher3.find()) {
            roleName = matcher3.group().split("<meta itemprop=\"name\" content=\"")[1].split(" - 原神WIKI_BWIKI_哔哩哔哩\"/>")[0];
            break;
        }
        System.out.println(roleName);


        Pattern compile = Pattern.compile("<div style=\"position: relative; overflow-x: scroll; border: 1px solid #bdbdbd;\"><table class=\"wikitable\" style=\"width:100%;max-width: 1300px; margin:0px;word-break: keep-all; border-collapse: separate;border-spacing:0; border:0;position:relative;\">[\\w\\W]*?LV13[\\w\\W]*?</div><table class=\"wikitable mw-collapsible mw-collapsed\" style=\"width:100%;display:none\">");
        Matcher matcher = compile.matcher(list.get(0));
        ArrayList<String> list1 = new ArrayList<>();
        while (matcher.find()) {
            list1.add(matcher.group());
        }
//        System.out.println(list1);


        ArrayList<String> list2 = new ArrayList<>();
        LinkedHashMap<String, ArrayList<String>> hashMap = new LinkedHashMap<>();
        int AEQ = 0;
        for (String s : list1) {
            Pattern compile1 = Pattern.compile("<tr><td style=\"font-weight:bold\">[\\w\\W]*?</tr>");
            Matcher matcher1 = compile1.matcher(s);
            while (matcher1.find()) {
                list2.add(matcher1.group());
            }
//            System.out.println(s);
            for (String s2 : list2) {
                Pattern compile12 = Pattern.compile("[\\u4e00-\\u9fa5]+");//匹配中文
                Matcher matcher12 = compile12.matcher(s2);
                String detailedProperties = null;
                while (matcher12.find()) {
                    if (AEQ == 0) {
                        detailedProperties = "A" + matcher12.group();
                    } else if (AEQ == 1) {
                        detailedProperties = "E" + matcher12.group();
                    } else if (AEQ == 2) {
                        detailedProperties = "Q" + matcher12.group();
                    }
                    break;
                }


                ArrayList<String> list3 = new ArrayList<>();
                Pattern compile2 = Pattern.compile("<td>[\\w\\W]*?</td>");//匹配倍率
                Matcher matcher2 = compile2.matcher(s2);
                while (matcher2.find()) {
                    String replace = matcher2.group().replace("<td>", "").replace("</td>", "").replace(" ", "");
                    if (replace.equals("")) {
                        continue;
                    }
                    list3.add(replace);
                }

                hashMap.put(detailedProperties, list3);
            }
            AEQ++;
        }

//        System.out.println(list2);


//        System.out.println(hashMap);

    }

    @Test
    void txtToList01() throws IOException {
        /*
            获取txt文件放到list集合中
        */
        File file = new File("D:\\JavaWebProjects\\AWoodenStakeHd\\src\\main\\resources\\txt\\charactersWebsitePage.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        String string = sb.toString();
        String[] strings = string.split("!@#!@#!@#!@#!@#");
        ArrayList<String> list = new ArrayList<>(Arrays.asList(strings));
//        System.out.println(list.size());






        /*
            正则表达式为：
            <div style="position: relative; overflow-x: scroll; border: 1px solid #bdbdbd;"><table class="wikitable" style="width:100%;max-width: 1300px; margin:0px;word-break: keep-all; border-collapse: separate;border-spacing:0; border:0;position:relative;">
            [\w\W]*?LV13[\w\W]*?
            </div><table class="wikitable mw-collapsible mw-collapsed" style="width:100%;display:none">
        */
//        System.out.println(list.get(0));
        LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> allMap = new LinkedHashMap<>();
        for (int iii = 0; iii < list.size(); iii++) {

            Pattern compile3 = Pattern.compile("<meta itemprop=\"name\" content=\"[\\u4e00-\\u9fa5()]+ - 原神WIKI_BWIKI_哔哩哔哩\"/>");
            Matcher matcher3 = compile3.matcher(list.get(iii));
            String roleName = null;
            while (matcher3.find()) {
                roleName = matcher3.group().split("<meta itemprop=\"name\" content=\"")[1].split(" - 原神WIKI_BWIKI_哔哩哔哩\"/>")[0];
                break;
            }
//            System.out.println(roleName);


            Pattern compile = Pattern.compile("<div style=\"position: relative; overflow-x: scroll; border: 1px solid #bdbdbd;\"><table class=\"wikitable\" style=\"width:100%;max-width: 1300px; margin:0px;word-break: keep-all; border-collapse: separate;border-spacing:0; border:0;position:relative;\">[\\w\\W]*?LV13[\\w\\W]*?</div><table class=\"wikitable mw-collapsible mw-collapsed\" style=\"width:100%;display:none\">");
            Matcher matcher = compile.matcher(list.get(iii));
            ArrayList<String> list1 = new ArrayList<>();
            while (matcher.find()) {
                list1.add(matcher.group());
            }
//        System.out.println(list1);


            /*

                //////////////////////////////////////////////////////////
                巨大错误
                //////////////////////////////////////////////////////////

            */

            LinkedHashMap<String, ArrayList<String>> hashMap = new LinkedHashMap<>();
            int AEQ = 0;
            for (String s : list1) {
                ArrayList<String> list2 = new ArrayList<>();
                Pattern compile1 = Pattern.compile("<tr><td style=\"font-weight:bold\">[\\w\\W]*?</tr>");
                Matcher matcher1 = compile1.matcher(s);
                while (matcher1.find()) {
                    list2.add(matcher1.group());
//                    System.out.println(matcher1.group());
//                    System.out.println();
                }


                for (String s2 : list2) {
                    Pattern compile12 = Pattern.compile("[\\u4e00-\\u9fa5]+");//匹配中文
                    Matcher matcher12 = compile12.matcher(s2);
                    String detailedProperties = null;
                    while (matcher12.find()) {
                        if (AEQ == 0) {
                            detailedProperties = "A" + matcher12.group();
                        } else if (AEQ == 1) {
                            detailedProperties = "E" + matcher12.group();
                        } else if (AEQ == 2) {
                            detailedProperties = "Q" + matcher12.group();
                        }
                        break;
                    }


                    ArrayList<String> list3 = new ArrayList<>();
                    Pattern compile2 = Pattern.compile("<td>[\\w\\W]*?</td>");//匹配倍率
                    Matcher matcher2 = compile2.matcher(s2);
                    while (matcher2.find()) {
                        String replace = matcher2.group().replace("<td>", "").replace("</td>", "").replace(" ", "");
                        if (replace.equals("")) {
                            continue;
                        }
                        list3.add(replace);
                    }

                    hashMap.put(detailedProperties, list3);
                }
                AEQ++;
            }
            allMap.put(roleName, hashMap);
        }

//        System.out.println(list2);


//        System.out.println(hashMap);


        Set<Map.Entry<String, LinkedHashMap<String, ArrayList<String>>>> entries = allMap.entrySet();
        boolean flag = false;
        for (Map.Entry<String, LinkedHashMap<String, ArrayList<String>>> entry : entries) {
//            System.out.println(entry.getKey() + " = " + entry.getValue()); // 闲云 = {...}
            System.out.println(JSONUtil.toJsonStr(entry));

//            if (Objects.equals(entry.getKey(), "莫娜")) {
//                flag = true;
//            }
//            if (Objects.equals(entry.getKey(), "刻晴")) {
//                flag = false;
//            }
//            if (flag) {
//                continue;
//            }


            Multiplier multiplier = new Multiplier();
            multiplier.setName(entry.getKey());


            LinkedHashMap<String, ArrayList<String>> value = entry.getValue();// {a1 = [], a2 = [], ...}

            LinkedHashMap<String, ArrayList<String>> valueA = new LinkedHashMap<>();
            LinkedHashMap<String, ArrayList<String>> valueE = new LinkedHashMap<>();
            LinkedHashMap<String, ArrayList<String>> valueQ = new LinkedHashMap<>();

            Set<Map.Entry<String, ArrayList<String>>> entries1 = value.entrySet();
            for (Map.Entry<String, ArrayList<String>> stringArrayListEntry : entries1) {
//                System.out.println(stringArrayListEntry.getKey()+"="+stringArrayListEntry.getValue());
//                System.out.println(JSONUtil.toJsonStr(stringArrayListEntry));

                if (stringArrayListEntry.getKey().contains("A")) {
                    valueA.put(stringArrayListEntry.getKey(), stringArrayListEntry.getValue());

                }
                if (stringArrayListEntry.getKey().contains("E")) {
                    valueE.put(stringArrayListEntry.getKey(), stringArrayListEntry.getValue());

                }
                if (stringArrayListEntry.getKey().contains("Q")) {
                    valueQ.put(stringArrayListEntry.getKey(), stringArrayListEntry.getValue());

                }

            }


            multiplier.setaSkill(JSONUtil.toJsonStr(valueA));
            multiplier.seteSkill(JSONUtil.toJsonStr(valueE));
            multiplier.setqSkill(JSONUtil.toJsonStr(valueQ));
//            multiplierMapper.addData(multiplier);


        }


    }

    @Test
    void txtToList03() {
        String html = "<div>第一个标签</div><div>第二个标签</div>";

        Pattern pattern = Pattern.compile("<div>(.*?)</div>");
        Matcher matcher = pattern.matcher(html);

        while (matcher.find()) {
            String content = matcher.group(1);
            System.out.println("匹配到的内容：" + content); // 输出：第一个标签
        }
    }

    @Test
    void txtToList04() {


        String json = "{\"莫娜\":{\"A一段伤害\":[\"37.6%\",\"40.4%\",\"43.2%\",\"47.0%\",\"49.8%\",\"52.6%\",\"56.4%\",\"60.2%\",\"63.9%\",\"67.7%\",\"71.4%\"],\"A二段伤害\":[\"36.0%\",\"38.7%\",\"41.4%\",\"45.0%\",\"47.7%\",\"50.4%\",\"54.0%\",\"57.6%\",\"61.2%\",\"64.8%\",\"68.4%\"],\"A三段伤害\":[\"44.8%\",\"48.2%\",\"51.5%\",\"56.0%\",\"59.4%\",\"62.7%\",\"67.2%\",\"71.7%\",\"76.2%\",\"80.6%\",\"85.1%\"],\"A四段伤害\":[\"56.2%\",\"60.4%\",\"64.6%\",\"70.2%\",\"74.4%\",\"78.6%\",\"84.2%\",\"89.9%\",\"95.5%\",\"101%\",\"107%\"],\"A重击伤害\":[\"150%\",\"161%\",\"172%\",\"187%\",\"198%\",\"210%\",\"225%\",\"240%\",\"255%\",\"269%\",\"285%\"],\"A重击体力消耗\":[\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\",\"50.0点\"],\"A下坠期间伤害\":[\"56.8%\",\"61.5%\",\"66.1%\",\"72.7%\",\"77.3%\",\"82.6%\",\"89.9%\",\"97.1%\",\"104%\",\"112%\",\"120%\"],\"A低空\":[\"114%/142%\",\"123%/153%\",\"132%/165%\",\"145%/182%\",\"155%/193%\",\"165%/206%\",\"180%/224%\",\"194%/243%\",\"209%/261%\",\"225%/281%\",\"240%/300%\"],\"E持续伤害\":[\"32.0%\",\"34.4%\",\"36.8%\",\"40.0%\",\"42.4%\",\"44.8%\",\"48%\",\"51.2%\",\"54.4%\",\"57.6%\",\"60.8%\",\"64%\",\"68%\"],\"E爆裂伤害\":[\"133%\",\"143%\",\"153%\",\"166%\",\"176%\",\"186%\",\"199%\",\"212%\",\"226%\",\"239%\",\"252%\",\"266%\",\"282%\"],\"E冷却时间\":[\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\",\"12.0秒\"],\"Q启动体力消耗\":[\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\",\"10.0点\"],\"Q持续体力消耗\":[\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\",\"每秒15.0点\"]}}\n";

    }

    /**
     * 倍率数据获取
     */
    @Test
    void test05() {

        LambdaQueryWrapper<Multiplier> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Multiplier::getId, Multiplier::getName, Multiplier::getaSkill, Multiplier::geteSkill, Multiplier::getqSkill)
                .eq(Multiplier::getName, "闲云");
        Multiplier multiplier = multiplierMapper.selectOne(wrapper);
        {
            String getaSkill = multiplier.getaSkill();
            LinkedHashMap<String, JSONArray> bean = JSONUtil.toBean(getaSkill, LinkedHashMap.class);
            LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<>();
            for (Map.Entry<String, JSONArray> entry : bean.entrySet()) {
                ArrayList<String> list = new ArrayList<>();
                for (Object o : entry.getValue()) {
                    String string = o.toString();
                    list.add(string);
                }
                map.put(entry.getKey(), list);
            }
            for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
        }
        {
            String geteSkill = multiplier.geteSkill();
            LinkedHashMap<String, JSONArray> bean = JSONUtil.toBean(geteSkill, LinkedHashMap.class);
            LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<>();
            for (Map.Entry<String, JSONArray> entry : bean.entrySet()) {
                ArrayList<String> list = new ArrayList<>();
                for (Object o : entry.getValue()) {
                    String string = o.toString();
                    list.add(string);
                }
                map.put(entry.getKey(), list);
            }
            for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
        }
        {
            String getqSkill = multiplier.getqSkill();
            LinkedHashMap<String, JSONArray> bean = JSONUtil.toBean(getqSkill, LinkedHashMap.class);
            LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<>();
            for (Map.Entry<String, JSONArray> entry : bean.entrySet()) {
                ArrayList<String> list = new ArrayList<>();
                for (Object o : entry.getValue()) {
                    String string = o.toString();
                    list.add(string);
                }
                map.put(entry.getKey(), list);
            }
            for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
                System.out.println(entry.getKey()+"="+entry.getValue());
            }
        }

    }

}
























































